QuantumBool#
- class QuantumBool(qs=None, name=None)[source]#
QuantumBools are the quantum type, which represents boolean truth values. They are the return type of comparison operators like the equality
==
.Apart from their behavior as a QuantumVariable, they can also be treated like ControlEnvironments.
Examples
We create a QuantumBool and set it to uniform superposition
>>> from qrisp import QuantumBool, h >>> q_bool_0 = QuantumBool() >>> h(q_bool_0) >>> print(q_bool_0) {False: 0.5, True: 0.5}
We create a second QuantumBool and evaluate some logical functions
>>> q_bool_1 = QuantumBool() >>> print(q_bool_1 | q_bool_0) {False: 0.5, True: 0.5} >>> print(q_bool_1 & q_bool_0) {False: 1.0}
QuantumBools are the results of comparisons:
>>> from qrisp import QuantumFloat, QuantumChar >>> q_ch = QuantumChar() >>> q_ch[:] = {"g" : 1, "l" : -1} >>> q_bool_2 = (q_ch == "g") >>> q_bool_2.qs.statevector() sqrt(2)*(|g>*|True> - |l>*|False>)/2
For QuantumFloats, numeric comparison is also possible:
>>> qf = QuantumFloat(4) >>> h(qf[3]) >>> print(qf) {0: 0.5, 8: 0.5} >>> q_bool_3 = (qf >= 4) >>> print(q_bool_3) {False: 0.5, True: 0.5}
To use a QuantumBool as a ControlEnvironment, we simply put it in a
with
statement:with q_bool_3: qf += 2
>>> print(qf) {0: 0.5, 10: 0.5}
QuantumBools that are created directly after a
with
statement are uncomputed automatically:with qf == 10: q_bool_3.flip()
>>> print(qf.qs)
QuantumCircuit: -------------- ┌────────────┐ ┌───────────┐ qf.0: ─────┤0 ├─────┤0 ├──o─────────o── │ │ │ │ │ │ qf.1: ─────┤1 ├─────┤1 ├──■─────────■── │ │ │ __iadd__ │ │ │ qf.2: ─────┤2 ├─────┤2 ├──o─────────o── ┌───┐│ less_than │ │ │ │ │ qf.3: ┤ H ├┤3 ├─────┤3 ├──■─────────■── └───┘│ │┌───┐└─────┬─────┘ │ ┌───┐ │ lt_qbl.0: ─────┤4 ├┤ X ├──────■────────┼──┤ X ├──┼── │ │└───┘ │ └─┬─┘ │ lt_ancilla.0: ─────┤5 ├────────────────────┼────┼────┼── └────────────┘ ┌─┴─┐ │ ┌─┴─┐ cond_env.0: ─────────────────────────────────────┤ X ├──■──┤ X ├ └───┘ └───┘ Live QuantumVariables: --------------------- QuantumFloat qf QuantumBool lt_qbl
Note that there is only a single QuantumBool listed in the “Live QuantumVariables” section, because the QuantumBool of the comparison
qf == 10
(calledcond_env
) has been uncomputed.
Methods#
Flips the QuantumBool's value. |